package com.document.controll;

import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.nutz.dao.Cnd;
import org.nutz.ioc.annotation.InjectName;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import com.document.model.User;

@IocBean
@InjectName
public class LoginAction extends BaseAction {
	
	@At("/login/auth")
	public void auth(@Param("userName") String userName,@Param("pwd") String pwd
			,HttpServletRequest rep,HttpServletResponse resp){
		User user = basicDao.findByCondition(User.class, Cnd.where("username", "=", userName).and("pwd", "=", pwd));
		String rvt = "{success:false}";
		if(user != null){
			rvt = "{success:true}";
			rep.getSession().setAttribute("userInfo", user);
		}
		super.write(resp, rvt);	
	}
	
	@Ok("jsp:pages.homePage")
	@At("/login/homePage")
	public void login(HttpServletRequest req,ServletContext context){
		User user = (User) req.getSession().getAttribute("userInfo");
		Map rrMap = (Map) context.getAttribute("roleRight");
		List resList = (List) rrMap.get(user.getUserType());
		req.setAttribute("resList", resList);
	}
}
